﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Election.BanToChucBinhBau
{
    public partial class ChonThanhVienUC : UserControl
    {
        private string _maDotBC = null;
        public string maDotBC 
        {
            get { return this._maDotBC; }
            set { this._maDotBC = value; }
        }
        public ChonThanhVienUC()
        {
            InitializeComponent();
        }

        public class Item
        {
            private string _text;
            private string _value;
            private bool _isReadOnly = false;
            public string Text
            {
                get
                {
                    return this._text;
                }
                set { this._text = value; }
            }
            public string Value
            {
                get { return this._value; }
                set { this._value = value; }
            }
            public bool isReadOnly
            {
                get { return this._isReadOnly; }
                set { this._isReadOnly = value; }
            }

            public Item()
            {

            }
            public Item(string text, string value)
            {
                this.Value = value;
                this.Text = text;
            }
            public Item(string text, string value, bool readOnly)
            {
                this.Value = value;
                this.Text = text;
                this.isReadOnly = readOnly;
            }
            public override string ToString()
            {
                return this.Text;
            }
        }
        private void ChonThanhVienUC_Load(object sender, EventArgs e)
        {
            string query = "select * from quanlybaucu.donvi";
            try
            {
                //donvi combobox initialized
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                this.donViCmbBox.Items.Clear();
                this.donViCmbBox.Items.Add(new Item("Tất cả", ""));
                for (int i = 0; i < dataTbl.Rows.Count; i++)
                {
                    Item item = new Item();
                    item.Text = dataTbl.Rows[i]["TENDV"].ToString();
                    item.Value = dataTbl.Rows[i]["MADV"].ToString();
                    this.donViCmbBox.Items.Add(item);
                }
                this.donViCmbBox.SelectedIndex = 0;

                //thanh vien combobox initialized
                query = "select * from quanlybaucu.thanhvien";
                DataTable tvTbl = orcl.ExecuteQueryThrowEx(query);
                if(tvTbl.Rows.Count == 0)
                {
                    MessageBox.Show("Không có thông tin thành viên nào trong hệ thống");
                    return;
                }                
                for(int i  =0; i<tvTbl.Rows.Count; i++)
                {
                    this.thanhVienLstBox.Items.Add(new Item(tvTbl.Rows[i]["MATV"].ToString() + "  --  " + tvTbl.Rows[i]["TEN"].ToString(), tvTbl.Rows[i]["MATV"].ToString()));
                }                

                //dotbaucu combobox initilized
                query = "select * from quanlybaucu.dotbaucu";
                DataTable dotBCTbl = orcl.ExecuteQueryThrowEx(query);
                if (dotBCTbl.Rows.Count == 0)
                {
                    MessageBox.Show("Không có thông tin đợt bầu cử nào trong hệ thống");
                    return;
                }
                for (int i = 0; i < dotBCTbl.Rows.Count; i++)
                {
                    this.dotBauCuCombBox.Items.Add(new Item(dotBCTbl.Rows[i]["MADOTBAUCU"].ToString(), dotBCTbl.Rows[i]["MADOTBAUCU"].ToString()));
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                    
        }

        private void addBut_Click(object sender, EventArgs e)
        {
            if (this.thanhVienLstBox.SelectedItems.Count == 0)
            {
                return;
            }
            if (this.ungCVLstBox.Items.Count >= 5)
            {
                MessageBox.Show("Mỗi đợt ứng cử chỉ chọn tối đa 5 ứng cử viên");
                return;
            }
            //add item from thanhvien to ungcuvien
            Item tv = (Item)this.thanhVienLstBox.SelectedItem;
                for (int j = 0; j < this.ungCVLstBox.Items.Count; j++)
                {
                    Item ucv = (Item)this.ungCVLstBox.Items[j];
                    if (tv.Value == ucv.Value)
                        goto EXITUCV;
                }
                this.ungCVLstBox.Items.Add(tv);                
            EXITUCV:
                {
                }
            }

        private void removeBut_Click(object sender, EventArgs e)
        {
            if (this.ungCVLstBox.SelectedItem != null)
            {
                if (!((Item)this.ungCVLstBox.SelectedItem).isReadOnly)
                    this.ungCVLstBox.Items.RemoveAt(this.ungCVLstBox.SelectedIndex);
                else
                    MessageBox.Show("Ứng cử viên này đã có sẵn trong hệ thống. Không thể loại bỏ khỏi danh sách");
            }
        }

        private void donViCmbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.donViCmbBox.SelectedItem == null)
            {
                return;
            }
            //change thanhvien list box
            string query = "select * from quanlybaucu.thanhvien ";
            Item selItem = (Item)this.donViCmbBox.SelectedItem;
            if (selItem.Value != "")
            {
                query = query + " where thuocdonvi = '" + selItem.Value + "' ";
            }
            this.thanhVienLstBox.Items.Clear();
            DataTable tvTbl = orcl.ExecuteQueryThrowEx(query);
            if (tvTbl.Rows.Count == 0)
            {
                MessageBox.Show("Không có thông tin thành viên nào trong hệ thống");
                return;
            }
            for (int i = 0; i < tvTbl.Rows.Count; i++)
            {
                this.thanhVienLstBox.Items.Add(new Item(tvTbl.Rows[i]["MATV"].ToString() + "  --  " + tvTbl.Rows[i]["TEN"].ToString(), tvTbl.Rows[i]["MATV"].ToString()));
            }   
        }

        private void thanhVienLstBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.thanhVienLstBox.SelectedItem == null)
            {
                return;
            }
            string matv = ((Item)this.thanhVienLstBox.SelectedItem).Value;
            string query = "select * from quanlybaucu.thanhvien tv join quanlybaucu.donvi dv on (tv.thuocdonvi = dv.madv) where tv.matv = '" + matv + "' ";
            DataTable thanhVien = orcl.ExecuteQueryThrowEx(query);
            if (thanhVien.Rows.Count == 0)
            {
                //CHECK HERE
                //set label to null
                return;
            }
            this.tenLbl.Text = thanhVien.Rows[0]["TEN"].ToString();
            this.phaiLbl.Text = thanhVien.Rows[0]["PHAI"].ToString();
            this.ngaySinhLbl.Text = thanhVien.Rows[0]["NGAYSINH"].ToString();
            this.queQuanLbl.Text = thanhVien.Rows[0]["QUEQUAN"].ToString();
            this.quocTichLbl.Text = thanhVien.Rows[0]["QUOCTICH"].ToString();
            this.dcThuongTruLbl.Text =  thanhVien.Rows[0]["DCTHUONGTRU"].ToString();
            this.dcTamTruLbl.Text = thanhVien.Rows[0]["DCTAMTRU"].ToString();
            this.donViLbl.Text = thanhVien.Rows[0]["TENDV"].ToString();
            this.chiNhanhLbl.Text = thanhVien.Rows[0]["CHINHANH"].ToString();
        }

        public List<String> getMaTV()
        {
            List<String> maTVArr =  new List<String>();
            for (int i = 0; i < this.ungCVLstBox.Items.Count; i++)
            {
                if (!((Item)this.ungCVLstBox.Items[i]).isReadOnly)
                    maTVArr.Add(((Item)this.ungCVLstBox.Items[i]).Value);
            }
            return maTVArr;
        }
        public void reload()
        {
            string query = "select * from quanlybaucu.donvi";
            try
            {
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                this.donViCmbBox.Items.Clear();
                this.donViCmbBox.Items.Add(new Item("Tất cả", ""));
                for (int i = 0; i < dataTbl.Rows.Count; i++)
                {
                    Item item = new Item();
                    item.Text = dataTbl.Rows[i]["TENDV"].ToString();
                    item.Value = dataTbl.Rows[i]["MADV"].ToString();
                    this.donViCmbBox.Items.Add(item);
                }
                this.donViCmbBox.SelectedIndex = 0;
                this.ungCVLstBox.Items.Clear();
                this.thanhVienLstBox.Items.Clear();
                query = "select * from quanlybaucu.thanhvien";
                DataTable tvTbl = orcl.ExecuteQueryThrowEx(query);
                if (tvTbl.Rows.Count == 0)
                {
                    MessageBox.Show("Không có thông tin thành viên nào trong hệ thống");
                    return;
                }
                for (int i = 0; i < tvTbl.Rows.Count; i++)
                {
                    this.thanhVienLstBox.Items.Add(new Item(tvTbl.Rows[i]["MATV"].ToString() + "  --  " + tvTbl.Rows[i]["TEN"].ToString(), tvTbl.Rows[i]["MATV"].ToString()));
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                    
        }

        private void removeAllBut_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.ungCVLstBox.Items.Count; i++)
            {
                Item item  = (Item)this.ungCVLstBox.Items[i];
                if (!item.isReadOnly)
                {
                    this.ungCVLstBox.Items.Remove(item);
                    i = i - 1;
                }
            }
        }

        private void dotBauCuCombBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.dotBauCuCombBox.SelectedItem != null)
            {
                string madbc = ((Item)this.dotBauCuCombBox.SelectedItem).Value;
                string query = "select tv.matv, tv.ten from quanlybaucu.ungcuvien ucv join quanlybaucu.dotbaucu dbc on (ucv.madotbaucu = dbc.madotbaucu) join quanlybaucu.thanhvien tv on (tv.matv = ucv.matv) where dbc.madotbaucu = '" + madbc + "' ";
                DataTable tbl = orcl.ExecuteQueryThrowEx(query);
                this.ungCVLstBox.Items.Clear();
                for (int i = 0; i < tbl.Rows.Count; i++)
                {
                    this.ungCVLstBox.Items.Add(new Item(tbl.Rows[i]["MATV"].ToString() + "  --  " + tbl.Rows[i]["TEN"].ToString() + " (ReadOnly)", tbl.Rows[i]["MATV"].ToString(), true));
                }               
                //CHECK HERE
                if (tbl.Rows.Count >= 5 )
                {
                    MessageBox.Show("Thông tin ứng cử viên của đợt bầu cử này đã được nhập đầy đủ vào hệ thống. Bạn không được phép nhập tiếp");
                    this.maDotBC = null;
                    return;
                }
                this.maDotBC = madbc;
            }
        }

        private void ungCVLstBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ungCVLstBox.SelectedItem == null)
            {
                return;
            }
            string matv = ((Item)this.ungCVLstBox.SelectedItem).Value;
            string query = "select * from quanlybaucu.thanhvien tv join quanlybaucu.donvi dv on (tv.thuocdonvi = dv.madv) where tv.matv = '" + matv + "' ";
            DataTable thanhVien = orcl.ExecuteQueryThrowEx(query);
            if (thanhVien.Rows.Count == 0)
            {
                //CHECK HERE
                //set label to null
                return;
            }
            this.tenLbl.Text = thanhVien.Rows[0]["TEN"].ToString();
            this.phaiLbl.Text = thanhVien.Rows[0]["PHAI"].ToString();
            this.ngaySinhLbl.Text = thanhVien.Rows[0]["NGAYSINH"].ToString();
            this.queQuanLbl.Text = thanhVien.Rows[0]["QUEQUAN"].ToString();
            this.quocTichLbl.Text = thanhVien.Rows[0]["QUOCTICH"].ToString();
            this.dcThuongTruLbl.Text = thanhVien.Rows[0]["DCTHUONGTRU"].ToString();
            this.dcTamTruLbl.Text = thanhVien.Rows[0]["DCTAMTRU"].ToString();
            this.donViLbl.Text = thanhVien.Rows[0]["TENDV"].ToString();
            this.chiNhanhLbl.Text = thanhVien.Rows[0]["CHINHANH"].ToString();
        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
               
}

